<?php
if ($action == 'import'):
    $js = <<<EOP
$('.form .btn-primary').click(function(){
    if ($('.form form').valid())
        $(this).hide();
});
EOP;

    cs()->registerScript(__CLASS__.'HideButton',$js);
endif;
$baseUrl = Yii::app()->assetManager->publish(Yii::getPathOfAlias('Shop.assets'));
?>
<style>
    .alert-error {display: none;}
</style>
<div class="form">
    <h1>Import Products Price</h1>
    <?php echo CHtml::beginForm('', 'post', array('enctype'=>'multipart/form-data','class'=>'form-horizontal'));?>

    <?php
    $allowImport = true;
    if (is_array($errors) && count($errors)):?>
        <div class="alert">
            <?php
            foreach ($errors as $error):?>
                <?php if (is_array($error) && isset($error['title'],$error['errors'])):
                    if (strpos($error['title'], 'attribute') !== false
                        || strpos($error['title'], 'Warning') !== false)
                        $allowImport = false;
                    ?>
                    <p><?php echo CHtml::tag('strong', array(), $error['title']).': ' .implode(', ',$error['errors']);?></p>
                <?php elseif (is_string($error)):?>
                    <p><?php echo $error;?></p>
                <?php endif;?>
            <?php endforeach;?>
        </div>
    <?php endif;?>
    <?php
    if ($action == 'import' && $allowImport)
        $this->renderPartial('importPrice_summary',array(
            'products'=>$total,
            'lastColumn' => $lastColumn,
            'lastRow' => $lastRow,
        ));
    ?>

    <?php
    if ($action == 'upload'):?>
        <div class="control-group">
            <label class="control-label">Choose File:</label>
            <div class="controls">
                <?php echo CHtml::fileField('file');?>
            </div>

            <p class="note" style="margin-left: 210px;">
                <?php echo CHtml::link('Click here', $baseUrl.'/Template/ImportProductTemplate.xls', array('title'=>'Click here','target'=>'_blank'));?> to download the template file to prepare your product data
            </p>
        </div>
    <?php endif;?>

    <?php if ($action == 'import' && $allowImport == true): ?>
        <div class="control-group import-process">
            <label class="control-label">File:</label>
            <div class="controls">
                <?php echo CHtml::textField('file', $file, array('readonly'=>'readonly'));?>
            </div>
        </div>
    <?php endif;?>

    <div class="control-group buttons">
        <div class="controls">
            <?php if ($action == 'upload'):?>
                <?php echo CHtml::submitButton('Upload', array('class' => 'btn btn-primary','name'=>'action')); ?>
            <?php elseif ($action == 'import' && $allowImport):?>
                <?php echo CHtml::submitButton('Import', array('class' => 'btn btn-primary','name'=>'action')); ?>
            <?php elseif ($allowImport == false):?>
                <?php echo CHtml::link('Back', array('/Shop/admin/product/importPrice'), array('class'=>'btn btn-primary','style'=>'color:white'));?>
            <?php endif;?>
        </div>
    </div>
    <?php echo CHtml::endForm();?>
</div>